﻿using QD.PushManager.DB;
using QD.PushManager.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QD.PushManager.Manager
{
    /// <summary>
    /// 订阅宝产品与小程序商品关联
    /// </summary>
    public class ProductUnionManager
    {
        static string TableName { get { return "sql_s003.dbo.vifo_product_union"; } }

        public static int Add(ProductUnion model)
        {

            using (var db = new ShtDatabase())
            {
                return db.Insert(model) == null ? 0 : 1;
            }
        }

        /// <summary>
        /// 获取订阅宝商品ID
        /// </summary>
        /// <param name="unionProductId"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static int GetProductIdByUnionId(int unionProductId, int source)
        {
            if (unionProductId < 1) return 0;

            using (var db = new ShtDatabase())
            {
                return db.ExecuteScalar<int>(string.Format("select top 1 productid from {0} where unionProductid={1} and source={2}", TableName, unionProductId, source));
            }
        }

        /// <summary>
        /// 获取会员的产品关联列表
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="source"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<ProductUnion> GetList(int userId, int source, int pageSize = 20)
        {
            if (userId < 1) return null;

            using (var db = new ShtDatabase())
            {
                return db.Fetch<ProductUnion>(string.Format("select top {3} * from {0} where userid={1} and source={2}", TableName, userId, source, pageSize));
            }
        }

        /// <summary>
        /// 获取关联ID列表
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="source"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<int> GetUnionIdList(int userId, int source, int pageSize = 20)
        {
            if (userId < 1) return null;

            using (var db = new ShtDatabase())
            {
                return db.Fetch<int>(string.Format("select top {3} UnionProductId from {0} where userid={1} and source={2}", TableName, userId, source, pageSize));
            }
        }

    }
}
